<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	
	class Post_new extends Admin_Controller{
		private $list_cat='';
		public function __construct(){
			parent::__construct();
			$this->load->model('database_model');
		}

		public function index(){
			if(!is_user_logged_in()){
				redirect('admin/login','refresh');
			}

			$terms = $this->database_model->Get('ci_terms')->result_array();
			$terms_tree = $this->categoryTree($terms);
			$user_id = $this->session->userdata('userid');
			$data = array(
				'header' => 'Add New Post',
				'menu_active' =>"3.2",
				'terms_tree' => $terms_tree,
				'user_id' => $user_id
				);
			$this->load->view('post_new', $data);
		}

		public function categoryTree($terms, $parent = 0){
			if(!is_user_logged_in()){
				redirect('admin/login', 'refresh');
			}
			$menu_tmp = array();
			foreach($terms as $key => $term){
				if((int) $term['term_parent'] == (int) $parent){
					$menu_tmp[] = $term;
					unset($terms[$key]);
				}
			}
			if($menu_tmp){
				$this->list_cat .= '<ul>';
				foreach($menu_tmp as $item){
					$this->list_cat .= '<li>';
					$this->list_cat .= '<label class="pointer" for="cate-'.$item['term_slug'].'"><input id="cate-'.$item['term_slug'].'" type="checkbox" name="terms[]" value="'.$item['term_id'].'">'.$item['term_name'].'</label>';
					$this->categoryTree($terms, $item['term_id']);
					$this->list_cat .= '</li>';
				}
				$this->list_cat .= '</ul>';
			}
			return $this->list_cat;
		}

		public function savePost(){
			if(!serverReferer()){
				redirect('admin/post_new', 'refresh');
			}
			$this->load->library('form_validation');
			$this->form_validation->set_rules('post_title', 'Post title', 'trim|required|min_length[2]|max_length[255]|xss_clean');
			if($this->form_validation->run() === FALSE){
				$json = array(
					'status' => '0',
					'message' => validation_errors()
					);
				echo json_encode($json);
			}else{
				$post_title    = $this->input->post('post_title', TRUE);
				$post_content  = $this->input->post('area');
				$post_date     = $this->input->post('post_date', TRUE);
				$post_author   = $this->input->post('post_author', TRUE);
				$post_status   = $this->input->post('post_status', TRUE);
				$post_featured = $this->input->post('post_featured', TRUE);

				$post_type     = 'post';
				
				$post_cate     = $this->input->post('terms', TRUE);

				if(empty($post_date)) $post_date = getCurrentDate();

				if(empty($post_author)) $post_author = 1;
				$data = array(
					'post_name'     => convertString($post_title),
					'post_title'    => $post_title,
					'post_content'  => $post_content,
					'post_author'   => $post_author,
					'post_type'     => 'post',
					'post_status'   => $post_status,
					'post_featured' => $post_featured,
					'post_date'     => $post_date,
					'post_date_mdf'     => $post_date,
					);
				
				try{
					$insert_post_id = $this->database_model->Insert('ci_posts', $data);
					// Insert ci_relations
					if(is_array($post_cate) && sizeof($post_cate)){
						foreach($post_cate as $term_id){
							$data = array(
								'post_id' => $insert_post_id,
								'term_id' => $term_id
								);
							$this->database_model->Insert('ci_relations', $data);
						}
					}else{
						$data = array(
							'post_id' => $insert_post_id,
							'term_id' => '1'
							);
						$this->database_model->Insert('ci_relations', $data);
					}
					$json = array(
						'status' => 1,
						'message' => 'post_edit/'.$insert_post_id
					);
					echo json_encode($json);
				}catch(Exceptions $e ){
					$json = array(
						'status' => 0,
						'message' => 'Error database. Try again'
					);
					echo json_encode($json);
				}
			}	
		}
	}
?>